package com.xjust.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xjust.Mapper.*;
import com.xjust.pojo.ResponseData;
import com.xjust.pojo.ResultBean;
import com.xjust.pojo.TechList;
import com.xjust.service.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class RegisterServiceImpl implements RegisterService {
    @Autowired
    private RegisterMapper registerMapper;
    @Autowired
    private CheckRequestMapper checkRequestMapper;
    @Autowired
    private MedicalTechnologyMapper medicalTechnologyMapper;
    @Autowired
    private InspectionRequestMapper inspectionRequestMapper;
    @Autowired
    private DisposalRequestMapper disposalRequestMapper;
    //获取最大病历号
    @Override
    public Integer getMaxCaseNumber() {
        return registerMapper.selectMaxCaseNumber()+1;
    }

    @Override
    public Integer getDoctorUsedNumber(Integer doctorId) {

        return registerMapper.selectDoctorUsedNumber(doctorId) ;
    }
//提交挂号信息
    @Override
    public void saveRegister(Map<String,Object> register) {
        register.put("visit_state",1);
        Boolean is_book = (Boolean) register.get("is_book");
        if(is_book){
            register.put("is_book","要");
        }else{
            register.put("is_book","否");
        }
      String birthday= (String) register.get("birthdate");
        birthday= birthday.substring(0,birthday.indexOf("T"));
        register.put("birthdate",birthday);
        String visit_date = (String) register.get("visit_date");
        System.out.println(visit_date);

       visit_date= visit_date.replace("T"," ").replace("Z","");
        System.out.println(visit_date);
        register.put("visit_date",visit_date);
        registerMapper.insertRegister(register);
    }
    //获取已看诊人数
    @Override
    public Integer getFinishPatientCount(Integer employeeId) {
        return registerMapper.selectCountByEmployeeIdAndVS(employeeId);
    }
//获取等待患者人数
    @Override
    public Integer getWaitPatientCount(Integer employeeId) {
        return registerMapper.selectWaitCountByEmployeeIdAndVS(employeeId);
    }
//获取等待患者
    @Override
    public ResultBean getWaitPatient(Integer employeeId, Integer page, Integer pageSize, String caseNumber, String realName,Integer visitState) {
        PageHelper.startPage(page,pageSize);
        Page<Map<String,Object>> pageInfo= registerMapper.selectWaitPatientByPage(employeeId,caseNumber,realName,visitState);
        ResultBean pageBean = new ResultBean();
        pageBean.setRows(pageInfo.getResult());
        pageBean.setCount(pageInfo.getTotal());
        return pageBean;
    }

    @Override
    public ResultBean getPatient(Integer page, Integer pageSize, String caseNumber,
                                 String realName) {
      Integer[] list =  checkRequestMapper.getCheckRequestId();
        PageHelper.startPage(page,pageSize);
        Page<Map<String,Object>> pageInfo= registerMapper.selectPatientByPage(list,caseNumber,realName);
        ResultBean pageBean = new ResultBean();
        pageBean.setRows(pageInfo.getResult());
        pageBean.setCount(pageInfo.getTotal());
        return pageBean;
    }
    //收费患者查看
    @Override
    public ResponseData getPatientToToll(Integer employeeId, String caseNumber, String realName) {
        ResponseData data = registerMapper.getPatientToToll(caseNumber, realName);
        for (TechList techList : data.getTechLists()) {
            techList.setCaseNumber(caseNumber);
        }
        return data ;
    }
    //提交收费并且修改对应状态
    @Override
    public void settlement(List<Map<String, Object>> tableData) {
        for (Map<String, Object> item : tableData) {
            System.out.println(item.get("techType"));
            if(item!=null){
                if("检查".equals(item.get("techType"))){
                    //根据病历号查找挂号表病人
                   Integer id = registerMapper.getRegisterIdByCaseNumber(item.get("caseNumber"));
                   //根据项目名查找项目ID
                    Integer medical_technology_id = medicalTechnologyMapper.getMedicalByName(item.get("techName"));
                    //前往检查表修改状态
                    checkRequestMapper.updateCheckRequestState(id,medical_technology_id);

                }else if("检验".equals(item.get("techType"))){
                    //根据病历号查找挂号表病人
                    Integer id = registerMapper.getRegisterIdByCaseNumber(item.get("caseNumber"));
                    //根据项目名查找项目ID
                    Integer medical_technology_id = medicalTechnologyMapper.getMedicalByName(item.get("techName"));
                    //前往检验表修改状态
                    inspectionRequestMapper.updateInspectionRequest(id,medical_technology_id);

                }else if("处置".equals(item.get("techType"))){
                    //前往处置表修改状态
                    //根据病历号查找挂号表病人
                    Integer id = registerMapper.getRegisterIdByCaseNumber(item.get("caseNumber"));
                    //根据项目名查找项目ID
                    Integer medical_technology_id = medicalTechnologyMapper.getMedicalByName(item.get("techName"));
                    //前往处置表修改状态
                    disposalRequestMapper.updateDisposalRequest(id,medical_technology_id);
                }else{
                        //前往挂号表修改状态
                        registerMapper.updateRegisterState(item.get("caseNumber"));

                }
            }
        }
    }

    @Override
    public List<Map<String, Object>> getPatientAll(String techType,String case_number,String real_name) {
        //获取当日检查、检验、处置表内所有患者
        if("检查".equals(techType)){
            //获取检查表内所有患者
          Integer[] ids =  checkRequestMapper.getCheckRequestOfId();
            //根据检查表查找挂号表
           return (registerMapper.getPatientInfo(ids,case_number,real_name));
        }else if("检验".equals(techType)){
            //获取检验表内所有患者
            Integer[] ids = inspectionRequestMapper.getInspectionInfo();
            return (registerMapper.getPatientInfo(ids,case_number,real_name));
        }else{
            //获取处置表内所有患者
            Integer[] ids = disposalRequestMapper.getDisposalRequestInfo();
            return (registerMapper.getPatientInfo(ids,case_number,real_name));
        }
    }

    //检查管理

}
